﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;

namespace jnlamp.Web.alarminfo
{
    /// <summary>
    /// GetAlarm 的摘要说明
    /// </summary>
    public class GetAlarm : IHttpHandler, IRequiresSessionState
    {
        jnlamp.BLL.alarminfo bll = new jnlamp.BLL.alarminfo();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "add")
                add(context);
            //else if (ac == "modify")
            //    modify(context);
            //else if (ac == "delete")
            //    delete(context);
            //else if (ac == "ddllist")
            //    ddllist(context);
        }       
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["caid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }        
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {                
                int fid = int.Parse(context.Request.Form["fid"]);
                int asid = int.Parse(context.Request.Form["asid"]);
                int fsid = int.Parse(context.Request.Form["fsid"]);
                string alevel = context.Request.Form["alevel"];
                string ainfo = context.Request.Form["ainfo"];
                DateTime adatetime = DateTime.Now ;               
                string atype = context.Request.Form["atype"];
                string suids = context.Request.Form["uid"];
                jnlamp.Model.alarminfo model = new jnlamp.Model.alarminfo();
                model.fid = fid;
                model.asid = asid;
                model.fsid = fsid;
                model.alevel = alevel;
                model.ainfo = ainfo;
                model.adatetime = adatetime;
                model.sid = uid;
                model.atype = atype;
                int alid=bll.Add(model);
                if ( alid> 0)
                {
                    //增加相关人员发送信息
                    jnlamp.BLL.alarmsendinfo ab = new BLL.alarmsendinfo();
                    jnlamp.Model.alarmsendinfo m = new Model.alarmsendinfo();
                    suids += "," + uid;//增加发给自己
                    string[] uids = suids.Split(',');
                    m.alid = alid;
                    foreach (string id in uids)
                    {
                        m.rid =int.Parse(id);
                        ab.Add(m);
                    }
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Query(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string uname, utruename, st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" mname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" mbh like '%{0}%' and ", utruename);
                }
                strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }  

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}